import Koa from 'koa'
import bodyparser from 'koa-bodyparser'
import router from './router/index'

const app = new Koa()

app.use(async (ctx, next) => {
  const start = Date.now();
  try {
    await next();
  } catch (error) {
    ctx.body = {
      code: 500,
      success: false
    }
  }
  const ms = Date.now() - start;
  console.log(`${ctx.method} ${ctx.url} - ${ms}ms`);
});

app.use(bodyparser())

// 加载路由中间件
app.use(router.routes()).use(router.allowedMethods())


app.listen(3000, () => {
    console.log('localhost:3000')
})
